package com.java83.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java83.dto.impl.OneDto;
import com.java83.entity.Leaving;
import com.java83.entity.LeavingReson;
import com.java83.response.ResponseEntity;
import com.java83.service.LeavingResonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张健聪
 * @program java-83-group-5-hou
 * @description 离职原因的controller
 * @date 2021-12-14 14:19
 **/
@RestController
@RequestMapping("/leavingReason")
public class LevavingResonController {
    @Autowired(required = false)
    LeavingResonService leavingResonService;

    /**
     * @param leavingReson 前端传来json
     * @return com.java83.response.ResponseEntity<java.lang.String>
     * @Description 离职原因新增
     * @Author 张健聪
     * @Date 2021/12/14 14:44
     */
    @RequestMapping("/add")
    public ResponseEntity<String> add(@RequestBody LeavingReson leavingReson) {
        try {
            int row = leavingResonService.add(leavingReson);
        } catch (Exception e) {

            return new ResponseEntity<String>(401, "添加失败", null);
        }
        return new ResponseEntity<String>(200, "添加成功", null);
    }


    /**
     * @param oneDto 前端dto数据
     * @return com.java83.response.ResponseEntity<com.github.pagehelper.PageInfo < com.java83.entity.LeavingReson>>
     * @Description 分页查询
     * @Author 张健聪
     * @Date 2021/12/14 15:32
     */
    @RequestMapping("/findAll")
    public ResponseEntity<PageInfo<LeavingReson>> findAll(@RequestBody OneDto<LeavingReson> oneDto) {
        List<LeavingReson> leavingResources = leavingResonService.findAll(oneDto.getData());
        PageHelper.startPage(oneDto.getPage(), oneDto.getLimit());
        PageInfo<LeavingReson> pageInfo = new PageInfo<LeavingReson>(leavingResources);
        return new ResponseEntity<PageInfo<LeavingReson>>(200, "查询成功", pageInfo);
    }

    /**
     * @param id 需要删除的id
     * @return com.java83.response.ResponseEntity<java.lang.String>
     * @Description 删除离职原因
     * @Author 张健聪
     * @Date 2021/12/14 16:21
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> delete(@PathVariable Integer id) {

        try {
            int row = leavingResonService.delete(id);
        } catch (Exception e) {
            return new ResponseEntity<String>(401, "删除失败", null);
        }
        return new ResponseEntity<String>(200, "删除成功", null);
    }

    /**
     * @Description 对离职原因进行更新
     * @param leavingReson 更新离职原因数据
     * @return com.java83.response.ResponseEntity<java.lang.String>
     * @Author 张健聪
     * @Date 2021/12/14 16:34
     */
    @RequestMapping("/update")
    public ResponseEntity<String> update(@RequestBody  LeavingReson leavingReson){

        try {
            int row = leavingResonService.update(leavingReson);
        } catch (Exception e) {

            return new ResponseEntity<String>(401, "修改失败", null);
        }
        return new ResponseEntity<String>(200, "修改成功", null);
    }
}
